local assets = {
	Asset("ANIM", "anim/musha_sword_base.zip"),
	Asset("ANIM", "anim/musha_sword_base_swap.zip")
}

local function Upgrade_to_mushasword(inst)
	local owner = inst.components.inventoryitem.owner
	local inv = inst.components.inventoryitem
	local musha_sword_fire = SpawnPrefab("musha_sword_fire")
	musha_sword_fire.exp = inst.components.musha_equipment.currentexp
	if inv ~= nil and owner ~= nil then
		local solt = owner.components.inventory:GetItemSlot(inst)
		owner.components.inventory:DropItem(inst)
		SpawnPrefab("statue_transition_2").Transform:SetPosition(owner:GetPosition():Get())
		if solt then
			owner.components.inventory:GiveItem(musha_sword_fire, solt)
		else
			owner.components.inventory:Equip(musha_sword_fire)
		end
	else
		SpawnPrefab("statue_transition_2").Transform:SetPosition(inst:GetPosition():Get())
		musha_sword_fire.Transform:SetPosition(inst:GetPosition():Get())
	end
	inst:Remove()
end

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.weapon:SetDamage(1)
		return
	end
	inst.components.weapon:SetDamage(18 + inst.components.musha_equipment.currentlevel * 2)
	if display then
		if inst.components.musha_equipment.currentlevel < 10 then
			inst.components.talker:Say(
				string.format(
					STRINGS.MUSHA_WEAPON_SWORD_BASE .. " (LV%d)\n" .. STRINGS.MUSHA_WEAPON_DAMAGE .. " (%d)",
					inst.components.musha_equipment.currentlevel,
					20 + inst.components.musha_equipment.currentlevel * 2
				)
			)
		else
			inst.components.talker:Say(
				string.format(
					STRINGS.MUSHA_WEAPON_SWORD_BASE_UP .. " \n" .. STRINGS.MUSHA_WEAPON_DAMAGE .. " (%d)",
					20 + inst.components.musha_equipment.currentlevel * 2
				)
			)
		end
	end
end

local function OnAttack(inst, attacker, target)
	if
		target.components.burnable and target.components.health and not target.components.health:IsDead() and
			not target:HasTag("structure") and
			not target:HasTag("stalkerminion") and
			not target:HasTag("smashable") and
			not target:HasTag("wall") and
			not target:HasTag("alignwall") and
			not target:HasTag("shadowminion") and
			inst.components.musha_equipment.currentlevel >= 10
	 then
		target.components.burnable:Ignite()
		target.components.health:DoDelta(-12)
	end
	if math.random() < 0.2 then
		inst.components.musha_equipment:Gain_Exp(1)
	end
end

local function OnEquip(inst, owner)
	--Updata(inst)

	owner.AnimState:OverrideSymbol("swap_object", "musha_sword_base_swap", "swap_nightmaresword")

	if inst.components.musha_equipment.currentlevel >= 10 then
		owner.AnimState:OverrideSymbol("swap_object", "musha_sword_fire_swap", "swap_nightmaresword")
	end
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
end

local function OnUnequip(inst, owner)
	--owner.fire = false
	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	inst.entity:AddLight()
	inst.Light:SetRadius(.4)
	inst.Light:SetFalloff(.5)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(80 / 255, 120 / 255, 250 / 255)
	inst.Light:Enable(true)

	inst.AnimState:SetBank("nightmaresword")
	inst.AnimState:SetBuild("musha_sword_base")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetMultColour(1, 1, 1, 0.7)

	inst:AddTag("sharp")
	inst:AddTag("aquatic")
	inst:AddTag("musha_items")

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		return inst
	end

	inst.type = "none"

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:SetMaxLevel(10, 130, Upgrade_to_mushasword)
	inst.components.musha_equipment:InitMushaEquip("weapon", 750, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			inst.components.weapon:SetDamage(1)
			SpawnPrefab("splash").Transform:SetPosition(inst:GetPosition():Get())
			inst.components.talker:Say(STRINGS.MUSHA_WEAPON_BROKEN .. " \nDamage (1)")
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_sword_base.xml"

	inst.BuffAttack = OnAttack

	return inst
end

return Prefab("musha_sword_base", fn, assets)
